package com.heima.media.service.impl;

import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.MediaEnum;
import com.heima.common.minio.service.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.entity.WmNewsMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.media.service.IWmNewsMaterialService;
import com.heima.media.service.IWmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-01-15
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {


    @Autowired
    private MinIOService minIOService;

    @Autowired
    private IWmNewsMaterialService wmNewsMaterialService;



    /**
     * 上传素材图片
     *
     * @param file 图片文件
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile file) {
        //1.从ThreadLocal中获取用户信息
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //2.调用MinIOService将图片上传到服务端
        String fileUrl = minIOService.upload(file);

        //3.构建素材对象 保存记录
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(user.getUserId());
        wmMaterial.setUrl(fileUrl);
        wmMaterial.setType(MediaEnum.MEDIA_MATERIAL_TYPE_IMG.getVal());
        this.save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }


    /**
     * 根据条件分页获取当前用户素材列表
     *
     * @param dto 是否收藏条件
     * @return 素材分页列表
     */
    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.封装查询条件对象
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //1.1 必须满足条件 用户ID
        queryWrapper.eq(WmMaterial::getUserId, user.getUserId());

        //1.2 收藏
        if (dto.getIsCollection() != null) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }

        //1.3 排序 上传时间倒序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //2.执行分页条件查询
        IPage<WmMaterial> page = this.page(new Page<>(dto.getPage(), dto.getSize()), queryWrapper);

        //3.封装响应分页数据
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    /**
     * 收藏素材 改变getIsCollection状态为1
     * 取消     改变getIsCollection状态为0
     * @param id
     * @param type
     * @return
     */
    @Override
    public ResponseResult updateStatus(Integer id, Integer type) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer userId = UserThreadLocalUtil.get().getUserId();
        LambdaUpdateWrapper<WmMaterial> qw = new LambdaUpdateWrapper<>();
        qw.set(WmMaterial::getIsCollection,type).eq(WmMaterial::getId,id).eq(WmMaterial::getUserId,userId);
        update(qw);
     * 素材收藏
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.根据素材id获取当前素材的数据
        WmMaterial material = this.getById(id);
        //2.更新素材中的收藏字段
        material.setIsCollection(true);
        this.updateById(material);
        return ResponseResult.okResult();
    }

    /**
     * 素材取消收藏
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel_collect(Integer id) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.根据素材id获取当前素材的数据
        WmMaterial material = this.getById(id);
        //2.更新素材中的收藏字段
        material.setIsCollection(false);
        this.updateById(material);
        return ResponseResult.okResult();
    }


    /**
     * 删除素材
     * @param id
     * @return
     */
    @Override
    public ResponseResult material(Integer id) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.对素材进行判断
        LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNewsMaterial::getMaterialId, id);
        List<WmNewsMaterial> list = wmNewsMaterialService.list(queryWrapper);
        //被应用的素材不能被删除
        if (list != null && !list.isEmpty()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_DELETE);
        } else {
            this.removeById(id);
        }
        return ResponseResult.okResult();
    }
}
